//
//  HomeKindViewController.m
//  geiTA
//
//  Created by duyan on 14-11-20.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HomeKindViewController.h"
#import "HomeXQTableViewCell.h"
#import "TitleXQViewController.h"
#import "HomeXQkindmodel.h"
#import "MJRefresh.h"
@interface HomeKindViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)  UITableView *table;
@end

@implementation HomeKindViewController
- (void)dealloc
{
    [_arr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [_page release];
    [self creattable];
    [self creatjson];
    [self setupRefresh];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    return self;
}
-(void)creattable
{
   self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height -49)style:UITableViewStylePlain];
    
    _table.delegate = self;
    _table.dataSource = self;
   
    [self.view addSubview:_table];
    [_table release];
}
-(void)creatjson
{
    NSURL *url = [NSURL URLWithString:self.string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic1 = [dic objectForKey:@"data"];
        self.page = [dic1 objectForKey:@"paging"];
        NSMutableArray *arr = [dic1 objectForKey:@"items"];
        for (NSMutableDictionary *dic in arr) {
            HomeXQkindmodel *model = [[HomeXQkindmodel alloc]init];
            [model  setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
            [_table reloadData];
            
        }
    }];

    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [_arr count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    static NSString *fy = @"in";
    
   HomeXQTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:fy];
    if (!cell) {
        cell = [[HomeXQTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:fy];
        
    }
    HomeXQkindmodel *model = [_arr objectAtIndex:indexPath.item];
        cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 3 ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TitleXQViewController *title = [[TitleXQViewController alloc]init];
    HomeXQkindmodel *model = [_arr objectAtIndex:indexPath.item];
    [title changurl:model.content_url];
    [title changeXQmodel:model];
    [self.navigationController pushViewController:title animated:YES];
    
    
}


-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.tabBarController.tabBar.hidden = NO;
      self.navigationController.navigationBar.translucent =NO;
}

-(void)change:(NSString *)URL
{
    self.string = URL;
}
#pragma mark-
#pragma mark 上拉加载下拉刷新
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.table addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    [self.table headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.table addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.table.headerPullToRefreshText = @"下拉可以刷新了";
    self.table.headerReleaseToRefreshText = @"松开马上刷新了";
    self.table.headerRefreshingText = @"正在帮你刷新中";
    
    // self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.table.footerReleaseToRefreshText = @"松开马上加载更多数据";
    self.table.footerRefreshingText = @"正在帮你加载中";
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    NSURL *url = [NSURL URLWithString:self.string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic1 = [dic objectForKey:@"data"];
        self.page = [dic1 objectForKey:@"paging"];
        NSMutableArray *arr = [dic1 objectForKey:@"items"];
        for (NSMutableDictionary *dic in arr) {
            HomeXQkindmodel *model = [[HomeXQkindmodel alloc]init];
            [model  setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
            [_table reloadData];
            
        }
    }];
    

    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [self.table reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table headerEndRefreshing];
    });
}

- (void)footerRereshing
{
    NSURL *url = [NSURL URLWithString:[self.page objectForKey:@"next"]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic1 = [dic objectForKey:@"data"];
        self.page = [dic1 objectForKey:@"paging"];
        NSMutableArray *arr = [dic1 objectForKey:@"items"];
        for (NSMutableDictionary *dic in arr) {
            HomeXQkindmodel *model = [[HomeXQkindmodel alloc]init];
            [model  setValuesForKeysWithDictionary:dic];
            [self.arr addObject:model];
            [_table reloadData];
        }
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.table reloadData];
        
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table footerEndRefreshing];
    });
    
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
